// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var playOpt = require('PlayOption');
var roundOpt = require('RoundOption');
var comps = require('ComponentDifine');
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        // radioBox: cc.Prefab,
        // checkBox: cc.Prefab,
        new_create_room: {
            default: null,
            type: cc.SpriteAtlas
        },
        labels: [cc.Label],
        rounds: [cc.Prefab],
        types: [cc.Prefab],
        //gruops: [cc.ToggleGroup]
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        var cfgRnd = roundOpt.mj;
        var cfgPlay = playOpt.fz;
        //var gCount = cfgRnd.groups + cfgPlay.groups;

        // for (let i = 0; i < gCount; i++) {
        //     var nodeGroup = new cc.Node('Group' + i);
        //     var g = nodeGroup.addComponent(cc.ToggleGroup);
        //     g.parent = this.node;
        //     this.gruops.push(g);
        // }

        // // 实例化预制
        // App.asset[comps.radio] = cc.instantiate(App.asset[comps.radio]);
        // this.checkBox = cc.instantiate(App.asset[comps.check]);

        this.initRound(cfgRnd);
        this.initPlayType(cfgPlay);
        // cc.loader.loadRes('component/RadioBox', (err, prefab) => {
        //     if (!err) {
        //     }
        // });

        // cc.loader.loadRes('component/CheckBox', (err, prefab) => {
        //     if (!err) {
        //     }
        // });
    },

    initRound(cfgRnd) {
        var lbls = cfgRnd.label;
        for (let i = 0; i < lbls.length; i++) {
            const l = lbls[i];

            var color = cc.Color.BLACK;
            color.fromHEX('#A9714F');
            var nodeLbl = new cc.Node('Label');
            nodeLbl.color = color;
            nodeLbl.x = l.x;
            nodeLbl.y = l.y;
            var lbl = nodeLbl.addComponent(cc.Label);
            lbl.string = l.text;
            //nodeLbl.parent = this.node;
            this.node.addChild(nodeLbl);
            this.labels.push(nodeLbl);
        }
        var opts = cfgRnd.round;

        for (let i = 0; i < opts.length; i++) {
            var r = opts[i];
            // console.log(comps.radio);
            // console.log(App.assets[comps.radio]);
            var op = cc.instantiate(App.assets[comps.radio]);
            op.setPosition(cc.p(r.x, r.y));
            op.getComponent('RadioBox').setLabel(r.text);
            op.getComponent('RadioBox').setGroup(r.group);
            //op.parent = this.node;
            this.node.addChild(op);
            this.rounds.push(op);
        }
    },

    initPlayType(cfgPlay) {
        var lbls = cfgPlay.labels;
        for (let i = 0; i < lbls.length; i++) {
            const l = lbls[i];
            var color = cc.Color.BLACK;
            color.fromHEX('#A9714F');
            var nodeLbl = new cc.Node('Label');
            nodeLbl.color = color;
            nodeLbl.x = l.x;
            nodeLbl.y = l.y;
            var lbl = nodeLbl.addComponent(cc.Label);
            lbl.string = l.text;
            //nodeLbl.parent = this.node;
            this.node.addChild(nodeLbl);
            this.labels.push(nodeLbl);
        }

        var typs = cfgPlay.types;

        for (let i = 0; i < typs.length; i++) {
            var t = typs[i];
            var op;
            if (t.isRadio) {
                op = cc.instantiate(App.assets[comps.radio]);
                op.getComponent('RadioBox').setLabel(t.text);
                op.getComponent('RadioBox').setGroup(t.group);
                op.setPosition(cc.p(t.x, t.y));
                // op.parent = this.node;
                // this.types.push(op);
            } else {
                op = cc.instantiate(App.assets[comps.check]);
                op.getComponent('CheckBox').setLabel(t.text);
                op.setPosition(cc.p(t.x, t.y));
                // op.parent = this.node;
                // this.types.push(op);
            }
            this.node.addChild(op);
            this.types.push(op);
        }
    },

    getPlayType: function () {
        var typeList = [];
        var cfgPlay = playOpt.fz;
        var typs = cfgPlay.types;

        for (let i = 0; i < this.types.length; i++) {
            var op = this.types[i];
            //console.log(op);
            var t = typs[i];
            var toggle = op.getChildByName('toggle');
            //console.log(toggle);
            if (toggle.getComponent(cc.Toggle).isChecked) {
                typeList.push(t.value)
            }
        }
        return typeList;
    },

    start() {

    },

    // update (dt) {},
});
